import 'package:flutter/material.dart';
import '../../api/api_service.dart';
import '../../provide/userProvide.dart';
import '../../routers/application.dart';
import 'package:provide/provide.dart';

class HomePage extends StatefulWidget {
  @override
  State createState() {
    return new _HomePageState();
  }
}

class _HomePageState extends State<HomePage> {
  List homeList = [];

  @override
  Widget build(BuildContext context) {
    _getHomeData();
    return Scaffold(
        backgroundColor: Color.fromRGBO(244, 245, 245, 1.0),
        appBar: AppBar(
          title: Text('校园共享单车'),
          actions: <Widget>[
            new IconButton(
                icon: new Icon(Icons.search),
                tooltip: '搜索',
                onPressed: () {
                  Application.router.navigateTo(context, "/searchOrder");
                })
          ],
        ),
        body: Container(
            decoration: BoxDecoration(
                border:
                    Border(right: BorderSide(width: 1, color: Colors.black12))),
            child: ListView.builder(
              itemCount: homeList.length,
              itemBuilder: (context, index) {
                return _leftInkWel(index);
              },
            )));
  }

  _getHomeData() async {
    try {
      await homeApi().then((data) async {
        if (data['success']) {
          setState(() {
            homeList = data['result'];
          });
        } else {
          showDialog(
              context: context,
              builder: (_) => new AlertDialog(content: new Text(data['msg'])));
        }
      });
    } catch (e) {
      print('登录异常');
      print(e);
    }
  }

  Widget _leftInkWel(int index) {
    return InkWell(
        child: Card(
      margin: EdgeInsets.all(10),
      child: Column(
        children: <Widget>[
          ListTile(
            title:
                Text(homeList[index]['name'], style: TextStyle(fontSize: 28)),
            subtitle: Text("编号"),
          ),
          Divider(),
          ListTile(
            title: Text("价格     ${homeList[index]['money']}/小时"),
          ),
          ListTile(
            title:
                Text("状态:   ${homeList[index]['state'] == true ? '正常' : '异常'}"),
          ),
          FlatButton(
            color: Colors.pink,
            child: Text(
              '租用',
              style:
                  TextStyle(color: Colors.white, backgroundColor: Colors.pink),
            ),
            onPressed: () {
              _postRentData(homeList[index]['_id']);
            },
          )
        ],
      ),
    ));
  }

  _postRentData(id) async {
    try {
      var userInfo = Provide.value<UserProvide>(context).userIofo;

      if (userInfo == null) {
        Application.router.navigateTo(context, "/login");
        return;
      }
      await rentBike({"bike_id": id, "user_id": userInfo.id}).then((data) {
        showDialog(
            context: context,
            builder: (_) => new AlertDialog(content: new Text(data['msg'])));
        if (data['success']) {
          print("租车成功");
          _getHomeData();
        }
      });
    } catch (e) {
      print('登录异常');
      print(e);
    }
  }
}
